package com.toman.Adpter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.toman.Entity.State_Entity;
import com.toman.MyApplication;
import com.toman.R;

import java.util.ArrayList;

/**
 * Created by  MyExpandableListViewAdapter on Administrator on 2017/11/27 on 10:34.
 */

public class StateExpandableListViewAdapter extends BaseExpandableListAdapter {

    private State_adpter gridViewAdapter = null;

    private Context mContext;

    /**
     * 每个分组的名字的集合
     */
    private ArrayList<String> groupList;

    /**
     * 所有分组的所有子项的 GridView 数据集合
     */
    private ArrayList<ArrayList<State_Entity>> itemList;

    private GridView gridView;
    MyApplication myApplication;

    public StateExpandableListViewAdapter(Context context, ArrayList<String> groupList, ArrayList<ArrayList<State_Entity>> itemList, MyApplication myApplication) {
        mContext = context;
        this.groupList = groupList;
        this.itemList = itemList;
        this.myApplication = myApplication;
    }

    @Override
    public int getGroupCount() {
        return groupList.size();
    }

    //itemList.get(groupPosition).size()
    @Override
    public int getChildrenCount(int groupPosition) {
        return 1;
    }

    @Override
    public Object getGroup(int groupPosition) {
        return groupList.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return itemList.get(groupPosition).get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        if (null == convertView) {
            convertView = View.inflate(mContext, R.layout.expandablelist_group, null);
        }
        ImageView ivGroup = (ImageView) convertView.findViewById(R.id.iv_group);
        TextView tvGroup = (TextView) convertView.findViewById(R.id.tv_group);
        TextView tvCount = (TextView) convertView.findViewById(R.id.tv_count);
        tvCount.setText(itemList.get(groupPosition).size() + "台");

        ImageView mCfs = (ImageView) convertView.findViewById(R.id.mCfs);
        if (myApplication.getSp().getString("token", "").equals("toman")) {
            mCfs.setBackgroundResource(R.mipmap.cf1);
        } else if (myApplication.getSp().getString("token", "").equals("operater")) {
            mCfs.setBackgroundResource(R.mipmap.cf);
        } else if (myApplication.getSp().getString("token", "").equals("company")) {
            mCfs.setBackgroundResource(R.mipmap.cf3);
        }


        // 如果是展开状态，就显示展开的箭头，否则，显示折叠的箭头
        if (isExpanded) {
            ivGroup.setImageResource(R.mipmap.xiangxia);
        } else {
            ivGroup.setImageResource(R.mipmap.xiangshang);
        }
        // 设置分组组名
        tvGroup.setText(groupList.get(groupPosition));
        if (groupList.get(groupPosition).equals("正常")) {
            mCfs.setBackgroundResource(R.mipmap.img_device_normal);
        }
        if (groupList.get(groupPosition).equals("空转")) {
            mCfs.setBackgroundResource(R.mipmap.img_device_free);

        }
        if (groupList.get(groupPosition).equals("待机")) {
            mCfs.setBackgroundResource(R.mipmap.img_device_wait);

        }
        if (groupList.get(groupPosition).equals("关闭")) {
            mCfs.setBackgroundResource(R.mipmap.img_device_off);
        }
        return convertView;
    }


    @Override
    public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        if (null == convertView) {
            convertView = View.inflate(mContext, R.layout.expandablelist_item, null);
        }
        // 因为 convertView 的布局就是一个 GridView
        // 所以可以向下转型为 GridView
        gridView = (GridView) convertView;
        // 创建 GridView 适配器
//gridView.setClickable(false);
//gridView.setPressed(false);
//gridView.setEnabled(false);
            gridViewAdapter = new State_adpter(mContext, itemList.get(groupPosition), myApplication);
            gridView.setAdapter(gridViewAdapter);


        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {

        return true;
    }


}
